package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.domain.LessonClassDO;
import com.bootdo.wx.domain.LessonContentDO;
import com.bootdo.wx.domain.LessonDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.LessonClassService;
import com.bootdo.wx.service.LessonContentService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-09 16:06:43
 */
 
@Controller
@RequestMapping("/wx/lessonContent")
public class LessonContentController {
	@Autowired
	private LessonContentService lessonContentService;
	@Autowired
	private LessonClassService lessonClassService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:lessonContent:lessonContent")
	String LessonContent(){
	    return "wx/lessonContent/lessonContent";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:lessonContent:lessonContent")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
		//查询列表数据
        Query query = new Query(params);
        if(params.containsKey("name")&&params.get("name")!=null&&!"".equals(params.get("name").toString())){
        	List<LessonContentDO> lessonContentList = lessonContentService.likeList(query);
        	if(lessonContentList.size()>0){
        		for (LessonContentDO lessonContentDO : lessonContentList) {
        			LessonClassDO lessonClassDO = lessonClassService.get(lessonContentDO.getClassId());
        			if(lessonClassDO!=null){
        				lessonContentDO.setClassId(lessonClassDO.getName());
        			}
        			GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(lessonContentDO.getGoodsId());
        			if(goodsGoodsDO!=null){
        				lessonContentDO.setGoodsId(goodsGoodsDO.getName());
        			}
				}
        	}
    		int total = lessonContentList.size();
    		PageUtils pageUtils = new PageUtils(lessonContentList, total);
    		return pageUtils;
        }
		List<LessonContentDO> lessonContentList = lessonContentService.list(query);
		if(lessonContentList.size()>0){
    		for (LessonContentDO lessonContentDO : lessonContentList) {
    			LessonClassDO lessonClassDO = lessonClassService.get(lessonContentDO.getClassId());
    			if(lessonClassDO!=null){
    				lessonContentDO.setClassId(lessonClassDO.getName());
    			}
    			GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(lessonContentDO.getGoodsId());
    			if(goodsGoodsDO!=null){
    				lessonContentDO.setGoodsId(goodsGoodsDO.getName());
    			}
			}
    	}
		int total = lessonContentService.count(query);
		PageUtils pageUtils = new PageUtils(lessonContentList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:lessonContent:add")
	String add(){
	    return "wx/lessonContent/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:lessonContent:edit")
	String edit(@PathVariable("id") String id,Model model){
		LessonContentDO lessonContent = lessonContentService.get(id);
		model.addAttribute("lessonContent", lessonContent);
		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", "0");
		map.put("sort", "location");
		map.put("order", "asc");
		map.put("status", "0");
		List<LessonClassDO> list = lessonClassService.list(map);
		model.addAttribute("lessonClassList", list);
		map.put("delFlag", "0");
		map.put("sort", "location");
		map.put("order", "asc");
		map.put("disabled", "0");
		List<GoodsGoodsDO> list2 = goodsGoodsService.list(map);
		model.addAttribute("goodsList", list2);
	    return "wx/lessonContent/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:lessonContent:add")
	public R save( LessonContentDO lessonContent){
	    lessonContent.setId(UUIDUtils.getUUID());
	    lessonContent.setDelFlag(0);
	    lessonContent.setCaAt(new Date());
	    lessonContent.setRecommend(1);
		if(lessonContentService.save(lessonContent)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:lessonContent:edit")
	public R update( LessonContentDO lessonContent){
		lessonContent.setUpAt(new Date());
		lessonContentService.update(lessonContent);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:lessonContent:remove")
	public R remove( String id){
		LessonContentDO lessonContent = lessonContentService.get(id);
		lessonContent.setDelFlag(1);
		lessonContent.setUpAt(new Date());
		if(lessonContentService.update(lessonContent)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:lessonContent:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				LessonContentDO lessonContent = lessonContentService.get(string);
				lessonContent.setDelFlag(1);
				lessonContent.setUpAt(new Date());
				lessonContentService.update(lessonContent);
			}
		}
	
		return R.ok();
	}
	//修改推荐文章页面
			@GetMapping("/resetRecommend/{id}")
			@RequiresPermissions("wx:lessonContent:edit")
			String resetRecommend(@PathVariable("id") String id,Model model){
				//获取视频信息
				LessonContentDO lessonContentDO = lessonContentService.get(id);
				model.addAttribute("lessonContentDO", lessonContentDO);
				return "wx/lessonContent/reset_recommend";
			}
			
			
			
			@ResponseBody
			@RequestMapping("/getLessonContent")
			public R getLessonContent() {
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("sort", "location");
				map.put("order", "asc");
				map.put("delFlag", "0");
				List<LessonContentDO> list = lessonContentService.list(map);
//				shopGoodsTypeParamgService.fetchLinks(list, "params", Cnd.orderBy().asc("location"));
				return R.ok().put("data", list);
			}

			/**
			 * 修改推荐文章
			 *
			 * @param
			 * @return
			 */
			@ResponseBody
			@PostMapping("/updateRecommend")
			public R updateRecommend(LessonContentDO lessonContentDO) {
				lessonContentService.update(lessonContentDO);
				return R.ok();
			}
}
